package ni;

import Presenter.Presenter;
import TCPFile.TCPReceive;
import TCPFile.TCPSend;
import UDPChat.UDPReceiver;
import UDPChat.UDPSender;
import chatsystemTDa2.*;
import java.io.File;
import java.io.IOException;
import java.net.*;

public class NI {
    
    Presenter presenter;
    
    private UDPSender _client;
    private UDPReceiver _server;
    private InetAddress _group;
    final int _port = 16050;
    final int _portTCP=3300;
    private boolean _actif;
    
    public NI(Presenter p) throws SocketException, InterruptedException, UnknownHostException {
        presenter = p;
        _group = InetAddress.getByName("255.255.255.255");
        _actif = false;
        _server = new UDPReceiver(this);
        _server.setName("UDP Receiver");
        _server.start();
        _client = new UDPSender(this);  
    }
    /*
    public void startServer(){
        _server.start();
    }*/
    
    public int getPort(){
        return _port;
    }
    
    public boolean isActiv(){
        return _actif;
    }
    
    public void setActiv(boolean b){
        _actif=b;
    }
    
    public String getLocalName(){
        return presenter.getModel().getLocalUser().getNom();
    }

    public int getPortTCP() {
        return _portTCP;
    }
    
    public void messageHandle(Message m, String remoteAddr) throws IOException
    {
        if(_actif){
            
            if(m instanceof Hello) {
                presenter.processHello((Hello) m, remoteAddr);
            }
            else if(m instanceof HelloAck) {
                presenter.processHelloAck((HelloAck) m, remoteAddr);
            }
            else if(m instanceof Send) {
                presenter.processSend((Send) m, remoteAddr);
            }
            else if(m instanceof SendAck) {
                System.out.println("Not supported yet.");
            }
            else if(m instanceof Goodbye) {
                presenter.processGoodbye((Goodbye) m, remoteAddr);
            }
            else if(m instanceof FileRequest){
                presenter.processFileRequest((FileRequest) m, remoteAddr);
            }
            else if(m instanceof FileResponse){
                presenter.processFileResponse((FileResponse) m, remoteAddr);
            }
        }
    }
    
    public void sendHello(String nickname) {
        Message m;
        m = new Hello(nickname);
        _client.sendTo(m,_group.getHostAddress(),_port);
    }

    public void sendHelloAck(String nickname, String hostname) {
        Message m;
        m = new HelloAck(nickname);
        _client.sendTo(m,hostname,_port);    
    }
    
    public void sendSend(String text, int id, String hostname) {
        Message m;
        m = new Send(text, id);
        _client.sendTo(m,hostname,_port); 
    }

    public void sendSendAck(int id_message, String hostname) {
        System.out.println("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void sendGoodbye(String nickname) {
        Message m;
        m = new Goodbye(nickname);
        _client.sendTo(m,_group.getHostAddress(),_port); 
    }
    
   public void sendFileRequest(String name, String hostname) {
        Message m;
        m = new FileRequest(name);
        System.out.println("Send file request");
        _client.sendTo(m,hostname,_port); 
    }
    
    public void sendFileResponse(boolean response, String name, String hostname) {
        Message m;
        m = new FileResponse(response, name);
        _client.sendTo(m,hostname,_port); 
    }
    
    public void sendFile(File file, String hostname) {
        
        System.out.println("----Thread TCP Send created");
        TCPSend send=new TCPSend(file,_portTCP,hostname);
        send.start();
        System.out.println("file sended : "+file.getName());
    }

    void receiveFile(String file_name) throws IOException {
        System.out.println("----Thread TCP Receive created");
            
            TCPReceive receive=new TCPReceive(_portTCP,file_name);
            
            receive.start();
    }
}